package com.authine.cloudpivot.ext.kingdee.work;

import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.ext.constants.KingDeeConstant;
import com.authine.cloudpivot.ext.dto.kingDee.KingDeeBusiness;
import com.authine.cloudpivot.ext.dto.kingDee.request.*;
import com.authine.cloudpivot.ext.dto.voucher.KingDeeBasicStats;
import com.authine.cloudpivot.ext.enums.kingdee.BusinessType;
import com.authine.cloudpivot.ext.enums.kingdee.FormEnum;
import com.authine.cloudpivot.ext.enums.kingdee.KingDeeBasicStatsEnum;
import com.authine.cloudpivot.ext.enums.kingdee.OrganizationRiseEnum;
import com.authine.cloudpivot.ext.utils.kingdee.DateUtils;
import com.authine.hermes.app.launcher.response.Response;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: liwei
 * @DateTime: 2021/7/16
 * @Description: TODO
 */
@Slf4j
public class OrganizationDataWork extends BaseWork {


    public Response createMember(KingDeeMember kingDeeMember) {
        Response returnData = new Response();

        log.info("创建员工  app:{}", kingDeeMember);
        /*组装业务数据*/
        KingDeeEntity kingDeeEntity = KingDeeEntity.saveKing();
        ModelEntity modelEntity = ModelEntity.addBasic(baseConstant.cNumber, baseConstant.uNumber);
        KingDeeBusiness business = kingDeeMember.getBusiness();

        //  todo 需要判断时间数据是否完整
        modelEntity.setFName(kingDeeMember.getName());
        modelEntity.setFStaffNumber(kingDeeMember.getNumber());
        if (business.getTime() == null) {
            modelEntity.setFDate(DateUtils.dateToStr(new Date()));
        } else {
            modelEntity.setFDate(DateUtils.dateToStr(business.getTime()));
        }
        kingDeeEntity.setModel(Arrays.asList(modelEntity));
        try {
            doSaveKingDeeAndRecord(FormEnum.EMPINFO, kingDeeEntity, business);

        } catch (Exception e) {
            log.info(e.getMessage());
            returnData.setSuccess(false);
            returnData.setErrMessage(e.getMessage());
            return returnData;
        }

        returnData.setSuccess(true);
        log.info("创建员工  业务逻辑处理结束 <<<");

        return returnData;
    }


    public Response createDepartment(KingDeeMember kingDeeMember) {
        Response returnData = new Response();
        log.info("金蝶业务创建部门创建部门 请求参数  KingDeeMember:{}", kingDeeMember);
        /*组装业务数据*/
        KingDeeEntity kingDeeEntity = KingDeeEntity.saveKing();
        KingDeeBusiness business = kingDeeMember.getBusiness();
        // 创建组织，使用组织 默认100，100好像在金蝶里面代指固买
        ModelEntity modelEntity = ModelEntity.addBasic(baseConstant.cNumber, baseConstant.uNumber);
        if (business.getTime() == null) {
            modelEntity.setFDate(DateUtils.dateToStr(new Date()));
        } else {
            modelEntity.setFDate(DateUtils.dateToStr(business.getTime()));
        }
        modelEntity.setFName(kingDeeMember.getName());
        kingDeeEntity.setModel(Arrays.asList(modelEntity));

        try {
            doSaveKingDeeAndRecord(FormEnum.DEPARTMENT, kingDeeEntity, business);
        } catch (Exception e) {
            returnData.setSuccess(false);
            returnData.setErrMessage(e.getMessage());
            return returnData;
        }

        returnData.setSuccess(true);
        log.info("金蝶业务创建部门创建部门  业务逻辑处理结束 <<<");

        return returnData;
    }


    public Response createOrganization(KingDeeOrganization org) {
        log.info("凭证管理=========金蝶业务创建组织  KingDeeOrganization:{}", org);
        Response returnData = new Response();
        String companyName = org.getCompanyName();
        String id = org.getId();

        if (org.getVendorAndCustomer()) {
            //客户
            KingDeeBusiness business = new KingDeeBusiness(id, null, BusinessType.Customer, null);
            KingDeeEntity k = KingDeeEntity.saveKing();
            ModelEntity m = ModelEntity.addBasic(baseConstant.cNumber, baseConstant.uNumber);
            if (business.getTime() == null) {
                m.setFDate(DateUtils.dateToStr(new Date()));
            } else {
                m.setFDate(DateUtils.dateToStr(business.getTime()));
            }
            m.setFNumber(id);
            m.setFName(companyName);
            k.setModel(Arrays.asList(m));

            try {

                doSaveKingDeeAndRecord(FormEnum.CUSTOMER, k, business);

                //金蝶基础数据表 保存一条数据
//                saveKingDeeBasicStats(id, companyName, KingDeeBasicStatsEnum.Customer);

            } catch (Exception e) {
                returnData.setSuccess(false);
                returnData.setErrMessage(e.getMessage());
                return returnData;
            }

        } else {

            createTransferVendor(new KingDeeTransferVendor(org.getId(), org.getCompanyName()));
        }

        Response response = new Response();
        response.setSuccess(true);
        log.info("凭证管理=========金蝶业务创建组织  业务逻辑处理结束 <<<");

        return response;
    }


    public Response createTransferVendor(KingDeeTransferVendor vendor) {
        log.info("凭证管理=========金蝶业务创建转采供应商 》》》 KingDeeOrganization:{}", vendor);

        Response returnData = new Response();

        String companyName = vendor.getCompanyName();
        String id = vendor.getId();

        //供应商
        KingDeeBusiness business = new KingDeeBusiness(id, null, BusinessType.Vendor, null);
        KingDeeEntity k = KingDeeEntity.saveKing();
        ModelEntity m = ModelEntity.addBasic(baseConstant.cNumber, baseConstant.uNumber);
        if (business.getTime() == null) {
            m.setFDate(DateUtils.dateToStr(new Date()));
        } else {
            m.setFDate(DateUtils.dateToStr(business.getTime()));
        }
        m.setFNumber(id);
        m.setFName(companyName);
        k.setModel(Arrays.asList(m));

        try {
            int i = 1;
            while (i < 3) {
                if (i == 1) {
                    business.setAccountName(OrganizationRiseEnum.GM.toString());
                    doSaveKingDeeAndRecord(FormEnum.SUPPLIER, k, business);
                } else if (i == 2) {
                    business.setAccountName(OrganizationRiseEnum.GM_HN.toString());
                    doSaveKingDeeAndRecord(FormEnum.SUPPLIER, k, business);
                }
                i++;
            }
            log.info("凭证管理=========金蝶业务创建转采供应商》》》逻辑结束");
        } catch (Exception e) {
            returnData.setSuccess(false);
            returnData.setErrMessage(e.getMessage());
            return returnData;
        }

        returnData.setSuccess(true);

        return returnData;
    }

    public void saveKingDeeBasicStats(String id, String companyName, KingDeeBasicStatsEnum type) {
        log.info("保存金蝶基础数据表 请求参数  id:{},companyName:{},type:{}", id, companyName, type);

        //todo 保存基础数据
        KingDeeBasicStats kingDeeBasicStats = new KingDeeBasicStats();
        kingDeeBasicStats.setCompany_name(companyName);
        kingDeeBasicStats.setKingdee_code(id);
        kingDeeBasicStats.setInfo_type(type);
        HashMap<String, Object> data = JSON.parseObject(JSON.toJSONString(kingDeeBasicStats), HashMap.class);

        String insert = kingDeeBasicStatsRepository.create(data);

        log.info("保存金蝶基础数据表 insert:{}", insert);
        log.info("保存金蝶基础数据表  业务逻辑处理结束 <<<");

    }


}
